package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.GoodWarehousingInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodWarehousingAdapter extends BaseAdapter {

    private Context context;
    private List<GoodWarehousingInfo.DataBean> list;
    private ViewHolder viewHolder;
    private Intent intent;
    private ClickListener mClickListener;
    private String pricePermission;// 門店查看價格權限

    public GoodWarehousingAdapter(Context context, List<GoodWarehousingInfo.DataBean> list) {
        this.context = context;
        this.list = list;
        ACache aCache = ACache.get(context);
        pricePermission = aCache.getAsString("showPrice");

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.good_warehousing_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        GoodWarehousingInfo.DataBean dataBean = list.get(position);
        if ("2".equals(pricePermission)) {
            viewHolder.layoutPMoneyGoodWarehousing.setVisibility(View.GONE);
            viewHolder.layoutPReallyMoneyGoodWarehousing.setVisibility(View.GONE);
            viewHolder.layoutPRefuseMoneyGoodWarehousing.setVisibility(View.GONE);
        }
        Tools.hideViewInItem(dataBean.getOperator(), viewHolder.peopleGoodWarehousing);
        Tools.hideViewInItem(dataBean.getAdd_time(), viewHolder.AddTimeGoodWarehousing);

        viewHolder.numGoodWarehousing.setText(R.string.status);
        viewHolder.statusGoodWarehousing.setText(dataBean.getExamine_name());
        viewHolder.peopleGoodWarehousing.setText(dataBean.getOperator());
        viewHolder.AddTimeGoodWarehousing.setText(dataBean.getAdd_time());
        viewHolder.commodityGoodWarehousing.setText(dataBean.getSupplier_name());
        viewHolder.timeGoodWarehousing.setText(dataBean.getAllocation_time());
        viewHolder.orderNoGoodWarehousing.setText(dataBean.getOrder_number());
        viewHolder.typeGoodWarehousing.setText(dataBean.getType_name());
        viewHolder.moneyGoodWarehousing.setText(dataBean.getMoney_total());
        viewHolder.refuseNumGoodWarehousing.setText(dataBean.getRefuse_goods_species());
        viewHolder.refuseMoneyGoodWarehousing.setText(dataBean.getRefuse_total_stock_money());
        Tools.hideViewInItem(dataBean.getReal_money_total(), viewHolder.reallyMoneyGoodWarehousing);
        if ("4".equals(dataBean.getType()) || "5".equals(dataBean.getType()) || "6".equals(dataBean.getType())) {
            viewHolder.detailGoodWarehousing.setVisibility(View.VISIBLE);
        } else {
            viewHolder.detailGoodWarehousing.setVisibility(View.INVISIBLE);
        }
        if ("1".equals(dataBean.getStatus())) {
            viewHolder.wareDetailGoodWarehousing.setVisibility(View.VISIBLE);
            viewHolder.viewGoodWarehousing.setVisibility(View.GONE);
            viewHolder.statusGoodWarehousing.setTextColor(context.getResources().getColor(R.color.complete_textColor));
        } else {
            viewHolder.wareDetailGoodWarehousing.setVisibility(View.INVISIBLE);
        }
        if (("4".equals(dataBean.getType()) || ("5".equals(dataBean.getType()) &&
                "1".equals(dataBean.getExamine_status()))) && "0".equals(dataBean.getStatus()) && "1".equals(dataBean.getRefuse_status())) {
            viewHolder.viewGoodWarehousing.setVisibility(View.VISIBLE);
            viewHolder.addGoodWarehousing.setVisibility(View.VISIBLE);
            viewHolder.statusGoodWarehousing.setTextColor(context.getResources().getColor(R.color.text_999));
        } else {
            viewHolder.addGoodWarehousing.setVisibility(View.GONE);
        }
        if (("4".equals(dataBean.getType()) || ("5".equals(dataBean.getType()) && "1".equals(dataBean.getExamine_status())))
                && "0".equals(dataBean.getStatus()) && "1".equals(dataBean.getRefuse_status())) {
            viewHolder.refuseGoodWarehousing.setVisibility(View.VISIBLE);
        } else {
            viewHolder.refuseGoodWarehousing.setVisibility(View.GONE);
        }
        if ("2".equals(dataBean.getRefuse_status())) {
            viewHolder.refuseDetailGoodWarehousing.setVisibility(View.VISIBLE);
        } else {
            viewHolder.refuseDetailGoodWarehousing.setVisibility(View.INVISIBLE);
        }
        Tools.hideViewInItem(dataBean.getRemarks(), viewHolder.tvRemark);
//        final int index = position;
        viewHolder.detailGoodWarehousing.setOnClickListener(v -> {  //  調撥詳情
            mClickListener.setClickListener(v, position);
        });
        viewHolder.wareDetailGoodWarehousing.setOnClickListener(v -> {  //  入庫詳情
            mClickListener.setClickListener(v, position);
        });
        viewHolder.addGoodWarehousing.setOnClickListener(v -> {   // 入庫
            mClickListener.setClickListener(v, position);
        });
        viewHolder.refuseGoodWarehousing.setOnClickListener(v -> {//拒收
            mClickListener.setClickListener(v, position);
        });
        viewHolder.refuseDetailGoodWarehousing.setOnClickListener(v -> {
            mClickListener.setClickListener(v, position);
        });
        return convertView;
    }


    public interface ClickListener {
        void setClickListener(View v, int position);
    }

    public void setmClickListener(ClickListener clickListener) {
        mClickListener = clickListener;
    }

    static class ViewHolder {
        @BindView(R.id.num_goodWarehousing)
        TextView numGoodWarehousing;
        @BindView(R.id.status_goodWarehousing)
        TextView statusGoodWarehousing;
        @BindView(R.id.people_goodWarehousing)
        TextView peopleGoodWarehousing;
        @BindView(R.id.layout_people_goodWarehousing)
        LinearLayout layoutPeopleGoodWarehousing;
        @BindView(R.id.time_goodWarehousing)
        TextView timeGoodWarehousing;
        @BindView(R.id.AddTime_goodWarehousing)
        TextView AddTimeGoodWarehousing;
        @BindView(R.id.layout_addTime_goodWarehousing)
        LinearLayout layoutAddTimeGoodWarehousing;
        @BindView(R.id.orderNo_goodWarehousing)
        TextView orderNoGoodWarehousing;
        @BindView(R.id.type_goodWarehousing)
        TextView typeGoodWarehousing;
        @BindView(R.id.commodity_goodWarehousing)
        TextView commodityGoodWarehousing;
        @BindView(R.id.money_goodWarehousing)
        TextView moneyGoodWarehousing;
        @BindView(R.id.layout_p_money_goodWarehousing)
        LinearLayout layoutPMoneyGoodWarehousing;
        @BindView(R.id.reallyMoney_goodWarehousing)
        TextView reallyMoneyGoodWarehousing;
        @BindView(R.id.layout_p_reallyMoney_goodWarehousing)
        LinearLayout layoutPReallyMoneyGoodWarehousing;
        @BindView(R.id.refuseNum_goodWarehousing)
        TextView refuseNumGoodWarehousing;
        @BindView(R.id.layout_p_refuseNum_goodWarehousing)
        LinearLayout layoutPRefuseNumGoodWarehousing;
        @BindView(R.id.refuseMoney_goodWarehousing)
        TextView refuseMoneyGoodWarehousing;
        @BindView(R.id.layout_p_refuseMoney_goodWarehousing)
        LinearLayout layoutPRefuseMoneyGoodWarehousing;
        @BindView(R.id.tv_remark)
        TextView tvRemark;
        @BindView(R.id.group_remark)
        LinearLayout groupRemark;
        @BindView(R.id.detail_goodWarehousing)
        TextView detailGoodWarehousing;
        @BindView(R.id.refuseDetail_goodWarehousing)
        TextView refuseDetailGoodWarehousing;
        @BindView(R.id.wareDetail_goodWarehousing)
        TextView wareDetailGoodWarehousing;
        @BindView(R.id.view_goodWarehousing)
        View viewGoodWarehousing;
        @BindView(R.id.refuse_goodWarehousing)
        TextView refuseGoodWarehousing;
        @BindView(R.id.add_goodWarehousing)
        TextView addGoodWarehousing;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
